package com.vstudy.zx.dao;

import com.vstudy.zx.dao.provider.ArticleDaoProvider;
import com.vstudy.zx.entity.Article;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Set;

public interface ArticleDao {

    @InsertProvider(type = ArticleDaoProvider.class, method = "insert")
    int insert(Article article);

    // 所有数据
    @Select("select * from tb_article ORDER BY article_release_time DESC limit #{skip}, #{pageSize}")
    @Results({
            @Result(column = "article_author", property = "author",
                    one = @One(select = "com.vstudy.zx.dao.TeacherDao.selectById"))
    })
    List<Article> selectPage(@Param("skip") Integer skip, @Param("pageSize") Integer pageSize);


    // 级联Teacher Outline
    @Select("select * from tb_article where article_id=#{id}")
    @Results({
            @Result(column = "article_author", property = "author",
                    one = @One(select = "com.vstudy.zx.dao.TeacherDao.selectById")),
            @Result(column = "outline_id", property = "outline",
                    one = @One(select = "com.vstudy.zx.dao.OutlineDao.selectById"))
    })
    Article selectById(Long id);

    @Select("call sp_delete_article(#{id})")
//    @DeleteProvider(type = ArticleDaoProvider.class, method = "delete")
    int delete(Long id);

//    int update(Article article);

    @Update("update tb_article set article_zan=article_zan+1 where article_id=#{article_id};")
    int addZan(Long article_id);

    @Update("update tb_article set article_zan=article_zan-1 where article_id=#{article_id};")
    int reduceZan(Long article_id);

    // 级联Teacher Outline
//    @Select("select * from tb_article where article_id in (1,2,3)")
    @SelectProvider(type = ArticleDaoProvider.class, method = "selectByIds")
    @Results({
            @Result(column = "article_author", property = "author",
                    one = @One(select = "com.vstudy.zx.dao.TeacherDao.selectById")),
            @Result(column = "outline_id", property = "outline",
                    one = @One(select = "com.vstudy.zx.dao.OutlineDao.selectById"))
    })
    List<Article> selectByIds(@Param("ids") Set<Long> ids);

    @Update("update tb_article set article_read_count=article_read_count+1 where article_id=#{article_id};")
    int addReadCount(Long article_id);

    // 根据标题模糊查询
    @Select("select * from tb_article where article_title like concat('%',#{key},'%')")
    @Results({
            @Result(column = "article_author", property = "author",
                    one = @One(select = "com.vstudy.zx.dao.TeacherDao.selectById"))
    })
    List<Article> selectByTitle(String key);

    @Select("select * from tb_article where outline_id=#{outlineId} limit 20")
    @Results({
            @Result(column = "article_author", property = "author",
                    one = @One(select = "com.vstudy.zx.dao.TeacherDao.selectById"))
    })
    List<Article> selectByOutlineId(Long outlineId);

    @Update("update tb_article set article_title=#{article_title} where article_id=#{article_id}")
    int update(Article article);
}
